//
//  KDSearchHomeController.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/23.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDSearchHomeController.h"
#import "KDSearchHistory.h"
#import "SVProgressHUD.h"
#import "KDSearchResultController.h"
#import "KDSBodyView.h"
#import "KDProvinceController.h"
#import "KDGetSaerchListParam.h"
#define PAGE_NUMBER 2
#define BodyTag 666
#define DURATION 0.8

@interface KDSearchHomeController ()< UIScrollViewDelegate, KDSBodyViewDelegate, UIGestureRecognizerDelegate, KDSearchResultControllerDelegate>
@property (nonatomic, weak) UITableView *tableView;
//@property (nonatomic, weak) UIView *navbar ;
@property (nonatomic, weak) UIScrollView *headView;

/***  被选中的主题btn*/
@property (nonatomic, weak) UIButton *selectBtn;
/***  装头部主题的arr */
@property (nonatomic, strong) NSArray *parentThemeArr;
/***  头部scrollView下面那个最大的scrollView*/
@property (nonatomic, weak) UIScrollView *bodyScrollView;
/***  当前的的搜索模型*/
@property (nonatomic, strong) KDSearchHistory *currentSearchModel;
/**
 *  当前的bodyView
 */
@property (nonatomic, weak) KDSBodyView *currentBodyView;
/**
 *  版本号
 */
@property (nonatomic, assign) NSInteger version;

@end

@implementation KDSearchHomeController
#pragma mark - 懒加载区
- (NSArray *)parentThemeArr{
    if (!_parentThemeArr) {
        _parentThemeArr = @[@"全网",@"微博"];
    }
    return _parentThemeArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    KDAccount *account = [KDAccountTool account];
    self.version = account.version;
//    if (!self.version) {//如果是非平台版,就不需要这个navBar
        [self setUpNavBar];
//    }
    
//    [self setUpHeadView];
    //添加手势
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
    hide.delegate = self;
    [hide addTarget:self action:@selector(hideTheKeyBoard)];
    [self.view addGestureRecognizer:hide];
}
- (void)setUpNavBar{
//    self.navigationController.navigationBar.hidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"leftArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(backViewClick)];
    [self setUpDetailScrollView];
}



- (void)setUpHeadView{
    //加一个挡板
    UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:view];
    //初始化underScrollView
    UIScrollView *headView = [[UIScrollView alloc]init];
    self.headView = headView;
    
    headView.backgroundColor = [UIColor clearColor];
    headView.showsHorizontalScrollIndicator = NO;
    headView.showsVerticalScrollIndicator = NO;
    headView.width = screenW * 0.7;
    headView.height = 44;
    self.navigationItem.titleView =headView ;
    //添加各主题子控件
//    [self setUpHeadBtn];
    
}
- (void)setUpHeadBtn
{
    for (int i = 0; i < self.parentThemeArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.width = self.headView.width / PAGE_NUMBER;
        btn.height = _headView.height;
        btn.x = i * btn.width ;
        btn.y = 0;
        btn.tag = 10+i;
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [btn setTitle:self.parentThemeArr[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(topicBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor: WQColor(203, 211, 221) forState:UIControlStateNormal];
        [btn setTitleColor: [UIColor whiteColor] forState:UIControlStateSelected];
        btn.backgroundColor = [UIColor clearColor];
        [_headView addSubview:btn];
        if (btn.tag == 10) {
            self.selectBtn = btn;
        }
    }
    
    [self setupDivider];
}
/**
 * 添加分割线
 */
- (void)setupDivider
{
    UIView *divider = [[UIView alloc] init];
//    divider.image = [UIImage imageNamed:@"timeline_card_bottom_line"];
    divider.backgroundColor = WQColor(69, 80, 94);
    [_headView addSubview:divider];
    divider.width = 0.8;
    divider.height = _headView.height -24;
    divider.x =  _headView.width *0.5;
    divider.y = 12;
//    KDLog(@"%@", NSStringFromCGRect(divider.frame));
//    [self.dividers addObject:divider];
}
- (void)setUpBodyScrollView
{
    if (!self.bodyScrollView) {
        UIScrollView * bodyScrollView = [[UIScrollView alloc]init];
        self.bodyScrollView = bodyScrollView;
        bodyScrollView.frame = CGRectMake(0, 0, screenW, screenH - CGRectGetMaxY(self.headView.frame)-44 -20);
        bodyScrollView.delegate = self;
        bodyScrollView.contentSize = CGSizeMake(screenW * PAGE_NUMBER, 1);
        bodyScrollView.showsHorizontalScrollIndicator = YES;
        bodyScrollView.scrollEnabled = NO;
        bodyScrollView.showsVerticalScrollIndicator = NO;
        bodyScrollView.showsHorizontalScrollIndicator = NO;
        bodyScrollView.backgroundColor = BackGroundColor;
        [self.view addSubview:bodyScrollView];
        //设置bodyView的排列
        [self setUpDetailScrollView];

    }
    
}
/**
 *  设置bodyView的排列
 */
- (void)setUpDetailScrollView
{
//    for (int i = 0; i < 2; i++) {
        KDSBodyView *body = [KDSBodyView bodyViewWithFrame:CGRectMake(0 * screenW, 0, screenW, screenH)];
        body.delegate = self;
        //        body.model = model;
        //        body.hasDetail = (model.info.count != 0);
        body.type = self.type;
        body.tag = BodyTag +self.type;
        KDLog(@"%ld", body.type);
        [self.view addSubview:body];
//        if (i == 0) {
//            self.currentBodyView = body;
//        }
//    }
    
}
/***  设置selectBtn的*/
- (void)setSelectBtn:(UIButton *)selectBtn
{
    _selectBtn.selected = NO;
    _selectBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    _selectBtn = selectBtn;
    _selectBtn.selected = YES;
    [UIView animateWithDuration:1.0f animations:^{
        _selectBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    }];
}
#pragma mark - 事件点击区
- (void)topicBtnDidClick:(UIButton *)btn{
    self.selectBtn = btn;
    NSInteger num = btn.tag -10;
    self.currentSearchModel.type = num;
    [self.bodyScrollView setContentOffset:CGPointMake(screenW * num, self.bodyScrollView.contentOffset.y) animated:YES];
    self.currentBodyView = (KDSBodyView *)[self.bodyScrollView viewWithTag:num+BodyTag];
    if (num) {//如果是在右边
        [UIView transitionWithView:self.bodyScrollView duration:DURATION options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil];
    }else{//如果是在左边
        [UIView transitionWithView:self.bodyScrollView duration:DURATION options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil];
    }
}
/**
 *  返回按钮被点击了
 */
- (void)backViewClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//- (void)searchBtnClick{
////    self.searchView.currentSearchModel

//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.bodyScrollView) {
         int num = (int)scrollView.contentOffset.x/screenW;
        self.currentSearchModel.type = num;
        UIButton *btn = (UIButton *)[self.headView viewWithTag:num +10];

        self.selectBtn = btn;
    }
//    KDLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}

#pragma mark - KDSBodyViewDelegate
/**
 *  cell被点击了
 */
- (void)KDSBodyView:(KDSBodyView *)bodyView WithModel:(KDGetSaerchListParam *)model didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KDSearchResultController *searchResult = [[KDSearchResultController alloc]init];
    searchResult.delegate = self;
    searchResult.param = model;
    searchResult.verSion = self.version;
    self.here = NO;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchResult animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
/**
 *  搜索按钮被点击了
 */
- (void)KDSBodyView:(KDSBodyView *)bodyView SearchBtnClickWithSearchModel:(KDGetSaerchListParam *)searchModel{
    
    KDSearchResultController *searchResult = [[KDSearchResultController alloc]init];
    searchResult.param = searchModel;
    self.here = NO;
    searchResult.delegate = self;
    searchResult.verSion = self.version;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchResult animated:YES];
    self.hidesBottomBarWhenPushed = NO;

}
/**
 *  地区选择按钮被点击了
 */
- (void)areaBtnClickWithBodyView:(KDSBodyView *)bodyView{
    KDProvinceController *vc = [[KDProvinceController alloc]init];
    self.here = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark - 隐藏键盘
- (void)hideTheKeyBoard
{
    [self.view endEditing:YES];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(nonnull UITouch *)touch{
    //若为UITableViewCellContentView，则不截取Touch事件
    if ([NSStringFromClass([touch.view class])isEqualToString:@"UITableViewCellContentView"] ||[NSStringFromClass([touch.view class])isEqualToString:@"KDSearchView"]) {
        return NO;
    }
    return YES;
}

#pragma mark - KDSearchResultControllerDelegate
- (void)searchResultCtrlDidPopBack:(KDSearchResultController *)searchResultCtrl{
    self.currentBodyView.back = YES;
}



@end
